package com.hjj.validation;

import com.hjj.beans.Stu;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class StuValidation implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        // 判定参数类型是否正确(如果是就进行验证)
        return Stu.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Stu temp = (Stu) target;
        if (temp.getId() < 1 || temp.getId() > 1000){
            errors.rejectValue("id", "stu.id", "默认的错误消息：id超标");
        }
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "stu.name", "默认的错误消息：姓名不能为空");

        if (temp.getScore() < 0 || temp.getScore() > 100){
            errors.rejectValue("score", "stu.score", "默认的错误消息：score超标");
        }

    }
}
